/**
 * Created by Administrator on 2017/5/24 0024.
 */
$(function(){
    /*导航栏*/
$(".nav_inner ul li").on("mouseenter",function(){

    $(this).find("a").css("color","red").parent("li").siblings().find("a").css("color","");
    $(".nav_inner ul li .reguar").css("color","");

    $(this).children(".submenu").stop().fadeIn(300).css("display","block");
})

$(".nav_inner ul li").on("mouseleave",function(){

    $(this).children(".submenu").stop().fadeOut(300,function(){
        $(this).css("display","none");
    });
    $(this).find("a").css("color","");
})


    /*点击购物车图标显示*/
    $(".shop_a:eq(0)").click(function(){

        $(".shopshow").animate({"right":"40px"},500);
    })

    /*点击x图标隐藏*/
    $(".shopshow .stitle .p2").click(function(){

        $(".shopshow").animate({"right":"-300px"},500);
    })

    /*返回顶部*/
    $(".shop_a:eq(5)").on("click",function(){

        var timer=setInterval(function(){
            //1,current
            var current=document.body.scrollTop||document.documentElement.scrollTop;

            //2.speed
            var speed=(0-current)/8;
            speed=Math.floor(speed);

            //3.临界值
            if(current==0){
                clearInterval(timer);
                return;
            }

            //运动
            document.documentElement.scrollTop=document.body.scrollTop=current+speed;

        },20)
    })


    $.ajax({
        url: "json/products.json",
        type: "get",
        dataType: "json",
        success:function(resu){
            var obj=resu.products;
            console.log(obj[0]);
            console.log(obj[0].proid);

            var pid = window.location.search.split('?')[1];
            for (var i=0;i<obj.length;i++){
                if (obj[i].proid==pid){
                 var index = i;
                 console.log(index);
                //console.log(obj[index].proname)
                }

            }

            console.log(pid);

            var tmpleft='<div class="main_l_t">';
            tmpleft+='<div class="aside">';

            tmpleft+='</div>';
            tmpleft+='<div class="main_l">';
            tmpleft+='<div><img src="img/goods-local.png"/></div>';
            tmpleft+='<div class="second"><img src='+obj[index].lefturl+'/></div>';
            tmpleft+='<a>变焦</a>';
            tmpleft+='</div>';
            tmpleft+='</div>';
            $(".main_inner").html($(".main_inner").html()+tmpleft);

                var tmphtml='<div class="main_r">';
                tmphtml+='<span class="shoptitle"><a class="shoptitle_t">'+obj[index].proname+'</a>'+obj[index].bnef+'</span><div class="clear"></div>';
                tmphtml+='<span class="delivery_bg"><img src="img/local-car.png"/></span><p class="delivery">极速仓库发货</p><div class="clear"></div>';
                tmphtml+='<div class="shopconten">'+obj[index].description+'</div>';

                tmphtml+='<div class="shop_price">';
                tmphtml+='<div style="margin-top:6px;"><span class="price">价格</span><span class="price_mon">￥245.00</span></div>';
                tmphtml+='<span class="price_cu">促销价</span><span class="price_mon1">'+obj[index].price+'</span><span class="discount">8.1折</span>';
                tmphtml+='</div>';

                tmphtml+='<div class="returnme"><span class="return_bg"><img src="img/return.png"/></span><span class="return">最高可返登录<a href="javascript:void(0);">登陆后查看</a>蜜豆</span></div>';

                tmphtml+='<div class="shoppdetail">';
                tmphtml+='<div class="detail1"><span>邮费</span><span>满99元邮费</span></div>';
                tmphtml+='<div class="detail2"><span>评价</span> <div>****</div> <a href="javascript:void(0);">(已有11人评价)</a></div>';
                tmphtml+='</div>';

                tmphtml+='<div class="shopcolor">';
                tmphtml+='<p>颜色分类*</p>';
                tmphtml+='<select class="opation">';
                tmphtml+='<option value>--请选择分类--</option>';
                tmphtml+='<option value="8124">'+obj[index].color1+'</option>';
                tmphtml+='<option value="8125">'+obj[index].color2+'</option>';
                tmphtml+='</select>';

                tmphtml+='<div class="amount" onselectstart="return false">';
                tmphtml+='<div class="amount_m"><span>数量</span><a class="reduce">-</a><input type="text" value='+obj[index].cartnumber+'><a class="add">+</a><span style="margin-left:10px;font-size:14px;color:#8c8c8c;">库存：<a style="color:red;font-size:14px">有货</a></span></div>';
                tmphtml+='<input type="button" value="加入收藏" class="addlike"><input type="button" value="加入购物车" class="addcart">';
                tmphtml+='</div>';
                tmphtml+='</div>';
                tmphtml+='</div>';

                $(".main_inner").html($(".main_inner").html()+tmphtml);
            }
    })

    $("body").on("click",".addcart",function(event){
        event.stopPropagation();

        var addcar = $(this);
        var img = $(".main_l").find('.second').attr('src');

        cname();


        //信息存入cookie
        var flyer = $("<img class='u-flyer'/>"); //创建img节点, 给一个class=u-flyer
        flyer.attr("src",img); //给img一张图

        //调用fly, 让图片飞
        flyer.fly({
            //起始位置
            start: {
                left: event.clientX,
                top: event.clientY,
                width: 90,
                height: 90
            },
            //结束位置
            end: {
                left: $("#cart").offset().left- $(window).scrollLeft(),
                top: $("#cart").offset().top- $(window).scrollTop()+80,

                width: 0,
                height:0

            },
            //结束后
            onEnd: function(){
                flyer.fadeOut();
                console.log("加入购物车成功!");
            }

        });

        /*创建商品*/
        var shopcart='<div class="cart_bot">';
        shopcart+='<div class="cart_po">极速仓</div>';
        shopcart+='<ul class="list5">';
        shopcart+='<li>';
        shopcart+='<div class="pro_bg"><a href="javascript:void(0);"><img src="'+obc[iner].url+'"/></a></div>';
        shopcart+='<div class="produ_r">';
        shopcart+='<div class="item_tit">'+obc[iner].proname+'</div>';
        shopcart+='<div class="add_po">';
        shopcart+='<a class="reg_t"><i class="reg"></i></a>';
        shopcart+='<span class="pro_num">'+obc[iner].cartnumber+'</span>';
        shopcart+='<a class="reg_t"><i class="addg"></i></a>';
        shopcart+='</div>';
        shopcart+='<div class="price_pro">';
        shopcart+='<span>'+obc[iner].price+'</span><span class="del"></span>';
        shopcart+='</div>';
        shopcart+='</div>';
        shopcart+='</li>';
        shopcart+='</ul>';
        shopcart+='</div>';
        $(".cart_content").append(shopcart);

        /*商品存入cookie*/
        function cname () {
            var d=new Date();
            d.setDate(d.getDate()+30);
            var arr =setCookie("cart",obc[iner].proid,d)
            console.log(arr);
        }

    });




    /*减号点击事件*/
    $("main").on("click",".reduce",function(e){
        var evt=e||event;
        evt.preventDefault();
        if(parseInt($(this).next("input").val())==1){
            $(this).next("input").attr("disable",true);
            return;
        }
        $(this).next().val(parseInt($(this).next("input").val())-1);
    })

    /*加号点击事件*/
    $("main").on("click",".add",function(e){
        var evt=e||event;
        evt.preventDefault();
        $(this).prev().val(parseInt($(this).prev("input").val())+1);
    })

})